package com.example.myapplication.tack_07;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.R;
import com.example.myapplication.data.ThemeData;
import com.example.myapplication.tack_07.adapter.ListAdapter;
import com.example.myapplication.tack_07.adapter.ThreeAdapter;
import com.example.myapplication.tack_07.data.BannerData;
import com.example.myapplication.tack_07.data.ListData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;

public class Youth extends AppCompatActivity {
    private ImageView exit;
    private ViewPager banner;
    private List<BannerData.DataDTO> bannerList;
    private RecyclerView three;
    private RecyclerView list;
    private View view;
    private LinearLayout line;
    private List<ListData.RowsDTO> tabulationlist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_07_youth);
        init();
        eixt_();
        banner_();
        tabulation();
    }

    private void tabulation() {
        String url = "prod-api/api/youth-inn/youth-inn/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ListData listData =(ListData)obj;
                tabulationlist = listData.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tabulationAdapter();
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void tabulationAdapter() {
        ListAdapter listAdapter = new ListAdapter(tabulationlist, Youth.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Youth.this);
        list.setLayoutManager(linearLayoutManager);
        list.setAdapter(listAdapter);
    }


    private void banner_() {
        String url = "prod-api/api/youth-inn/talent-policy-area/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData) obj;
                bannerList = bannerData.getData();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (BannerData.DataDTO dataDTO : bannerList) {
                            list.add(dataDTO.getImgUrl());
                        }
                        BannerTool bannerTool = new BannerTool(Youth.this,list,banner);
                        banner.setAdapter(bannerTool);
                        ThreeBanner(bannerData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ThreeBanner(BannerData bannerData) {
        ThreeAdapter threeAdapter = new ThreeAdapter(bannerList,Youth.this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(Youth.this,3);
        three.setAdapter(threeAdapter);
        three.setLayoutManager(gridLayoutManager);

    }

    private void eixt_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Youth.this.finish();
            }
        });
    }
    private void init() {
        banner = findViewById(R.id.banner);
        exit = findViewById(R.id.exit);
        three = findViewById(R.id.three);
        list = findViewById(R.id.list);
        view = LayoutInflater.from(Youth.this).inflate(R.layout.theme,null);
    }
}